package com.rpay.admin.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.rpay.admin.controller.base.BaseController;
import com.rpay.common.dto.user.UserRippleDTO;
import com.rpay.common.dto.user.UserTradeDTO;
import com.rpay.common.enums.PcsResultCode;
import com.rpay.common.vo.PcsResult;
import com.rpay.model.user.UserTradeDO;
import com.rpay.service.user.impl.UserIncomeServiceImpl;
import com.rpay.service.user.impl.UserServiceImpl;
import com.rpay.service.user.impl.UserTradeServiceImpl;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台交易管理controller
 */
@RestController
@RequestMapping("/api/trade")
public class TradeController {
    @Autowired
    private UserTradeServiceImpl userTradeService;

    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private UserIncomeServiceImpl userIncomeService;

    @GetMapping("/list")
    public PcsResult getTradeList(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, String type,
                                  String status, String startDate, String endDate) {
        Map param = new HashMap();
        param.put("type", type);
        param.put("status", status);
        param.put("startDate", startDate);
        param.put("endDate", endDate);

        RowBounds rowBounds = new RowBounds((page - 1) * pageSize, pageSize);
        return PcsResult.build(userTradeService.getUserTradeDataList(param, rowBounds));
    }

    /**
     * xrp 充值提现的列表统计
     * @param page
     * @param pageSize
     * @param type
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("/XRPList")
    public PcsResult getXRPInOutList(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, String type,
                                    String startDate, String endDate) {
        Map param = new HashMap();
        param.put("type", type);

        param.put("startDate", startDate);
        param.put("endDate", endDate);

        RowBounds rowBounds = new RowBounds((page - 1) * pageSize, pageSize);
        return PcsResult.build(userTradeService.getXRPInOutDataList(param, rowBounds));
    }


    /**
     * 交易审核
     *
     * @param param
     * @return
     */
    @PutMapping
    @Transactional
    public PcsResult audit(@RequestBody JSONObject param) {
        Integer id = param.getInteger("id");
        String status = param.getString("status");
        String type = param.getString("type");
        Integer walletId = param.getInteger("systemWallet");

        if (status.equalsIgnoreCase("FAIL")) {
            // 审核不通过
            // 只更改订单状态 其他不做操作
            UserTradeDO tempUserTradeDO = new UserTradeDO();
            tempUserTradeDO.setId(id);
            tempUserTradeDO.setStatus(status);
            userTradeService.updateTradeStatus(tempUserTradeDO);
            return PcsResult.build(PcsResultCode.PARAM_INVALID.getCode(), "此交易已被拒绝！");
        } else {

            if (type.equalsIgnoreCase("WITHDRAW")) {
                userTradeService.withdrawConfirm(id, walletId);
            } else if (type.equalsIgnoreCase("MANUAL_WITHDRAW")) {
                userTradeService.manualWithDrawConfirm(id);
            }

        }
        return new PcsResult();

    }

    @GetMapping("msg")
    public PcsResult getTradeMessage() {
        List<UserTradeDO> userTradeDOList = userTradeService.getPendingAuditList();
        return new PcsResult(userTradeDOList.size());
    }

}
